package com.sopt.friends;

import java.util.ArrayList;

import com.sopt.friends.sub.SubChatList;
import com.sopt.friends.sub.TabContentsActivity;
import com.sopt.friends.util.FriendsChatManager;
import com.sopt.friends.util.FriendsSms;
import com.sopt.friends.vo.FriendsChat;
import com.sopt.friends.vo.FriendsChatRoom;
import com.sopt.view.ChatListAdapter;
import com.sopt.view.ChatRoomListAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class ChatActivity extends Activity {
	private ListView ListView01;
	private Button createButton;
	private FriendsChatManager manager;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat);
		
		manager = new FriendsChatManager(this, getContentResolver());
		
		final String[] ss = manager.getMyFriendsPhoneList();
		
		ListView01 = (ListView) findViewById(R.id.listChat);
		
		ArrayList<FriendsChatRoom> infoMenuList = manager.getChatRoomList();
		
		for(FriendsChatRoom curRoom: infoMenuList){
			for(int i = 0; i<ss.length;i++){
				if(ss[i].equals(curRoom.getTargetnumber())){
					curRoom.setTargetname(ss[i]);
				}
			}
		}
		
		ChatRoomListAdapter infomenuAdapter = new ChatRoomListAdapter(ChatActivity.this, R.layout.row_chat_room_list, infoMenuList);

		ListView01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				toChatRoomFromList(((TextView) view.findViewById(R.id.chatRoomNumber)).getText().toString()
						,((TextView) view.findViewById(R.id.chatRoomName)).getText().toString());
			}
		});
		
		ListView01.setAdapter(infomenuAdapter);
		
		createButton = (Button) findViewById(R.id.btnCreateChat);
		
		createButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				createChatRoom();
			}
		});
		
		Button backButton = (Button) findViewById(R.id.backButton);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		
	}
	
	protected void createChatRoom() {
		final String[] arr = manager.getMyFriendsPhoneList();
		AlertDialog listdialog = new AlertDialog.Builder(
				ChatActivity.this).setTitle(
				"Choose one person").setItems(arr,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						toChatRoomFromDialog(arr[which]);
					}
				}).create();
		listdialog.show();
		
	}

	protected void toChatRoomFromDialog(String cur) {
		String curNumber = cur.substring(0,cur.indexOf("/"));
		String curName = cur.substring(cur.indexOf("/")+1);
		manager.createChatRoom(curNumber);
		Intent intent = new Intent(this, SubChatList.class);
		intent.putExtra("number", curNumber);
		intent.putExtra("name", curName);
		startActivity(intent);
	}
	protected void toChatRoomFromList(String number,String name){
		Intent intent = new Intent(this, SubChatList.class);
		intent.putExtra("number", number);
		intent.putExtra("name", name);
		startActivity(intent);
	}

	protected void onResume() {
		super.onResume();
	}
}
